home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / ddj0796.zip / cprog.796 < prev    next >
Text File  |  1996-05-22  |  11KB  |  362 lines

  1. _C PROGRAMMING COLUMN_
  2. by Al Stevens
  3.  
  4.  
  5. Listing One
  6.  
  7. // --- setup.h
  8. #include "resource.h"
  9.  
  10. class CSetupApp : public CWinApp
  11. {
  12.     CString m_strSourcePath;  // source path for install
  13. public:
  14.     CSetupApp() { }
  15.     const CString& GetSourcePath() const
  16.         { return m_strSourcePath; }
  17.     virtual BOOL InitInstance();
  18. };
  19. extern CSetupApp theApp;
  20.  
  21. Listing Two
  22.  
  23. // setup.cpp
  24. #include "stdafx.h"
  25. #include <io.h>
  26. #include "setup.h"
  27. #include "setupDlg.h"
  28.  
  29. CSetupApp theApp;
  30.  
  31. BOOL CSetupApp::InitInstance()
  32. {
  33.     Enable3dControlsStatic();
  34.     BOOL bPathOK = FALSE;
  35.     // --- make sure they are installing from the root path of CD-ROM
  36.     m_strSourcePath = m_pszHelpFilePath;
  37.     int nIndex = m_strSourcePath.ReverseFind('\\');
  38.     if (nIndex > 0)
  39.         m_strSourcePath = m_strSourcePath.Left(nIndex+1);
  40.     if (m_strSourcePath.GetLength() == 3)   {
  41.         if (m_strSourcePath.Right(2) == ":\\")  {
  42.             UINT nDriveType = GetDriveType(m_strSourcePath);
  43.             if (nDriveType == DRIVE_CDROM)
  44.                 bPathOK = TRUE;
  45.         }
  46.     }
  47.     if (!bPathOK)   {
  48.         AfxMessageBox("Install from CD-ROM only", MB_ICONSTOP);
  49.         return FALSE;
  50.     }
  51.     int len = 100;
  52.     int dirlen;
  53.     char* psysdir = 0;
  54.     do  {
  55.         dirlen = len;
  56.         delete psysdir;
  57.         psysdir = new char[dirlen+11];
  58.         int len = GetSystemDirectory(psysdir, dirlen);
  59.     } while (dirlen != len);
  60.     char* pnewdll = new char[dirlen+11];
  61.     strcpy(pnewdll, psysdir);
  62.     strcat(psysdir, "\\cygwin.dll");
  63.     strcat(pnewdll, "\\cygwin.sav");
  64.     if (_access(psysdir, 0) == 0)   {
  65.         CString msg(
  66.             "Setup has found a file named cygwin.dll"
  67.             " in the system directory.\n\n("
  68.         );
  69.         msg += CString(psysdir);
  70.         msg += ")\n\n";
  71.         msg += 
  72.   "This DLL can, and probably will, cause Quincy to malfunction.\n"
  73.   "Different versions of this DLL are incompatible with one another.\n"
  74.   "You must rename or remove this file before you run the tutorial.\n"
  75.   "Setup can rename the file to cygwin.sav if you wish.\n\n"
  76.   "Should Setup rename the file now?";
  77.         if (AfxMessageBox(msg, MB_YESNO | MB_ICONQUESTION) == IDYES)
  78.             rename(psysdir, pnewdll);
  79.     }
  80.     delete [] pnewdll;
  81.  
  82.     delete [] psysdir;
  83.     CSetupDlg dlg;
  84.     m_pMainWnd = &dlg;
  85.     dlg.DoModal();
  86.     return FALSE;
  87. }
  88.  
  89. Listing Three
  90.  
  91. // setupDlg.h
  92.  
  93. const LONG nDiskSpaceRequired = 40000; // space required in KB
  94. const INT nFileCount = 1000;           // number of files to copy
  95. class CSetupDlg : public CDialog
  96. {
  97.     static UINT StartCopies(void*);
  98.     void CopyFiles();
  99.     void CopyFiles(const CString& strDestination, const CString& strSource);
  100.     BOOL m_bCopying;
  101.     BOOL m_bAborting;
  102. public:
  103.     CSetupDlg(CWnd* pParent = NULL);
  104.     //{{AFX_DATA(CSetupDlg)
  105.     enum { IDD = IDD_SETUP_DIALOG };
  106.     CButton m_butInstall;
  107.     CProgressCtrl m_ctlProgress;
  108.     CString m_strDoing;
  109.     CString m_strPath;
  110. protected:
  111.     virtual void DoDataExchange(CDataExchange* pDX);
  112.     virtual LRESULT WindowProc(UINT message,WPARAM wParam,LPARAM Param);
  113. protected:
  114.     HICON m_hIcon;
  115.     virtual BOOL OnInitDialog();
  116.     afx_msg void OnPaint();
  117.     afx_msg HCURSOR OnQueryDragIcon();
  118.     afx_msg void OnInstall();
  119.     virtual void OnCancel();
  120.     DECLARE_MESSAGE_MAP()
  121. };
  122.  
  123. Listing Four
  124.  
  125. // --- setupDlg.cpp
  126. #include "stdafx.h"
  127. #include <direct.h>
  128. #include <io.h>
  129. #include <sys\stat.h>
  130. #include "setup.h"
  131. #include "setupDlg.h"
  132.  
  133. CSetupDlg::CSetupDlg(CWnd* pParent /*=NULL*/) 
  134.           : CDialog(CSetupDlg::IDD, pParent)
  135. {
  136.     m_strDoing = _T("");
  137.     m_strPath = _T("");
  138.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  139.     m_bCopying = FALSE;
  140.     m_bAborting = FALSE;
  141. }
  142. void CSetupDlg::DoDataExchange(CDataExchange* pDX)
  143. {
  144.     CDialog::DoDataExchange(pDX);
  145.     DDX_Control(pDX, IDC_INSTALL, m_butInstall);
  146.     DDX_Control(pDX, IDC_PROGRESS, m_ctlProgress);
  147.     DDX_Text(pDX, IDC_DOING, m_strDoing);
  148.     DDX_Text(pDX, IDC_PATH, m_strPath);
  149. }
  150. BEGIN_MESSAGE_MAP(CSetupDlg, CDialog)
  151.     ON_WM_PAINT()
  152.     ON_WM_QUERYDRAGICON()
  153.     ON_BN_CLICKED(IDC_INSTALL, OnInstall)
  154. END_MESSAGE_MAP()
  155. BOOL CSetupDlg::OnInitDialog()
  156. {
  157.     CDialog::OnInitDialog();
  158.     SetIcon(m_hIcon, TRUE);         // Set big icon
  159.     SetIcon(m_hIcon, FALSE);        // Set small icon
  160.     m_strPath = "C:\\DDJTUTOR";
  161.     UpdateData(FALSE);
  162.     return TRUE;
  163. }
  164. void CSetupDlg::OnPaint() 
  165. {
  166.     if (IsIconic())
  167.     {
  168.         CPaintDC dc(this);
  169.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  170.         int cxIcon = GetSystemMetrics(SM_CXICON);
  171.         int cyIcon = GetSystemMetrics(SM_CYICON);
  172.         CRect rect;
  173.         GetClientRect(&rect);
  174.         int x = (rect.Width() - cxIcon + 1) / 2;
  175.         int y = (rect.Height() - cyIcon + 1) / 2;
  176.         dc.DrawIcon(x, y, m_hIcon);
  177.     }
  178.     else
  179.         CDialog::OnPaint();
  180. }
  181. HCURSOR CSetupDlg::OnQueryDragIcon()
  182. {
  183.     return (HCURSOR) m_hIcon;
  184. }
  185. void CSetupDlg::OnInstall() 
  186. {
  187.     UpdateData(TRUE);
  188.     // ----- edit path information
  189.     char drive = toupper(m_strPath.GetAt(0));
  190.     char path[] = "c:\\";
  191.     path[0] = drive;
  192.  
  193.     if (drive < 'A' || drive > 'Z' || 
  194.             m_strPath[1] != ':' || m_strPath[2] != '\\')  {
  195.         AfxMessageBox(
  196.    "Specify target drive and directory (e.g. C:\\DDJTUTOR)", MB_ICONSTOP);
  197.         return;
  198.     }
  199.     UINT nDriveType = GetDriveType(path);
  200.     if (nDriveType != DRIVE_REMOVABLE && nDriveType != DRIVE_FIXED) {
  201.         AfxMessageBox("Install to local hard disk only", MB_ICONSTOP);
  202.         return;
  203.     }
  204.     // ----- test for available disk space
  205.     m_strDoing = "Checking for available disk space";
  206.     UpdateData(FALSE);
  207.     DWORD nSectorsPerCluster;
  208.     DWORD nBytesPerSector;
  209.     DWORD nNumberOfFreeClusters;
  210.     DWORD nTotalNumberOfClusters;
  211.     GetDiskFreeSpace(path, &nSectorsPerCluster, &nBytesPerSector,
  212.                            &nNumberOfFreeClusters, &nTotalNumberOfClusters);
  213.     LONG space =
  214.        (nNumberOfFreeClusters * (nSectorsPerCluster * nBytesPerSector)) / 1024;
  215.     if (space < nDiskSpaceRequired)  {
  216.         CString msg;
  217.         msg.Format(
  218.            "Tutorial requires %ldKB\n"
  219.            "%c: has only %ldKB", nDiskSpaceRequired, *path, space);
  220.         AfxMessageBox(msg, MB_ICONSTOP);
  221.         m_strDoing = "";
  222.         UpdateData(FALSE);
  223.         return;
  224.     }
  225.     // ---- enough space, verify installation request
  226.     CString strMsg("Install to ");
  227.     strMsg += m_strPath + "?";
  228.     if (AfxMessageBox(strMsg, MB_YESNO | MB_ICONQUESTION) != IDYES){
  229.         m_strDoing = "";
  230.         UpdateData(FALSE);
  231.         return;
  232.     }
  233.     // ---- if path ends with \ delete it
  234.     if (m_strPath.Right(1) == "\\") {
  235.         if (m_strPath.Right(2) == ":\\")    {
  236.             // ---- discourage installation to the root directory
  237.             if (AfxMessageBox(
  238.               "Do you really want to install into the root directory?",
  239.                MB_ICONQUESTION) == IDNO)
  240.                 return;
  241.         }
  242.         else
  243.             m_strPath = m_strPath.Left(m_strPath.GetLength()-1);
  244.     }
  245.     // ----- test to see if installation subdirectory exists
  246.     if (_chdir(m_strPath) == 0)  {
  247.         CString msg(m_strPath);
  248.         msg += " already exists.\nUse it?";
  249.         if (AfxMessageBox(msg, MB_YESNO | MB_ICONQUESTION) != IDYES) {
  250.             m_strDoing = "";
  251.             UpdateData(FALSE);
  252.             return;
  253.         }
  254.     }
  255.     m_butInstall.EnableWindow(FALSE);
  256.     m_ctlProgress.SetRange(0, nFileCount);
  257.     m_ctlProgress.SetStep(1);
  258.     m_bCopying = TRUE;
  259.     m_strDoing = "Copying:";
  260.     UpdateData(FALSE);
  261.     AfxBeginThread(&CSetupDlg::StartCopies, this);
  262. }
  263. UINT CSetupDlg::StartCopies(void* This)
  264. {
  265.     ((CSetupDlg*)This)->CopyFiles();
  266.     return 0;
  267. }
  268. void CSetupDlg::CopyFiles()
  269. {
  270.     CopyFiles(m_strPath, theApp.GetSourcePath() + "DDJTUTOR");
  271.     if (m_bCopying)
  272.         AfxMessageBox("Setup completed");
  273.     else 
  274.         AfxMessageBox(
  275.             "Note: Setup did not run to completion.\n"
  276.